package com.hghivln.erpwarehouse.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.erpwarehouse.business.entity.Customer;
import com.hghivln.erpwarehouse.business.entity.Goods;
import com.hghivln.erpwarehouse.business.entity.Sales;
import com.hghivln.erpwarehouse.business.entity.Salesback;
import com.hghivln.erpwarehouse.business.mapper.SalesbackMapper;
import com.hghivln.erpwarehouse.business.service.CustomerService;
import com.hghivln.erpwarehouse.business.service.GoodsService;
import com.hghivln.erpwarehouse.business.service.SalesService;
import com.hghivln.erpwarehouse.business.service.SalesbackService;
import com.hghivln.erpwarehouse.business.vo.SalesbackVo;
import com.hghivln.erpwarehouse.system.common.lang.ActiverUser;
import com.hghivln.erpwarehouse.system.common.lang.DataGridlView;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-07-13
 */
@Service
public class SalesbackServiceImpl extends ServiceImpl<SalesbackMapper, Salesback> implements SalesbackService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SalesService salesService;
    
    @Override
    public DataGridlView queryAllSalesback(SalesbackVo salesbackVo) {
        IPage<Salesback> page=new Page<>(salesbackVo.getPage(),salesbackVo.getLimit());
        QueryWrapper<Salesback> qw=new QueryWrapper<>();
        qw.eq(salesbackVo.getGoodsid()!=null,"goodsid",salesbackVo.getGoodsid());
        qw.eq(salesbackVo.getCustomerid()!=null,"customerid",salesbackVo.getCustomerid());

        qw.ge(salesbackVo.getStartDate()!=null,"salesbacktime",salesbackVo.getStartDate());
        qw.le(salesbackVo.getEndDate()!=null,"salesbacktime",salesbackVo.getEndDate());

        this.getBaseMapper().selectPage(page,qw);
        List<Salesback> records = page.getRecords();
        for (Salesback record : records) {
            if(null!=record.getGoodsid()){
                Goods goods = this.goodsService.getById(record.getGoodsid());
                record.setSize(goods.getSize());
                record.setGoodsname(goods.getGoodsname());
            }
            if(null!= record.getCustomerid()){
                Customer customer = this.customerService.getById(record.getCustomerid());
                record.setCustomername(customer.getCustomername());
            }
        }
        return new DataGridlView(page.getTotal(),records);
    }

    @Override
    public Salesback saveSalesback(Salesback salesback) {

        Sales sales = salesService.getById(salesback.getSalesid());
        Goods goods = goodsService.getById(sales.getGoodsid());

        // 增加库存 保存退货信息
        goods.setNumber(goods.getNumber()+salesback.getNumber());
        goodsService.updateGoods(goods);

        // 更新销售单
        sales.setNumber(sales.getNumber()-salesback.getNumber());
        salesService.updateById(sales);

        salesback.setPaytype(sales.getPaytype());
        salesback.setSalebackprice(sales.getSaleprice());
        salesback.setCustomerid(sales.getCustomerid());
        salesback.setSalesbacktime(new Date());
        ActiverUser activerUser = (ActiverUser) SecurityUtils.getSubject().getPrincipal();
        salesback.setOperateperson(activerUser.getUser().getName());

        getBaseMapper().insert(salesback);
        return salesback;
    }
}
